package mblog.web.controller.site.posts;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import mblog.base.data.Data;
import mblog.modules.blog.data.PostVO;
import mblog.modules.blog.service.ChannelService;
import mblog.modules.blog.service.PostService;
import mblog.modules.user.data.AccountProfile;
import mblog.web.controller.BaseController;

@Controller
@RequestMapping({"/post"})
public class PostController
  extends BaseController
{
  @Autowired
  private PostService postService;
  @Autowired
  private ChannelService channelService;
  
  @GetMapping({"/editing"})
  public String view(Long id, ModelMap model)
  {
    model.put("channels", this.channelService.findAll(0));
    if ((id != null) && (id.longValue() > 0L))
    {
      AccountProfile profile = getSubject().getProfile();
      PostVO view = this.postService.get(id.longValue());
      
      Assert.notNull(view, "该文章已被删除");
      Assert.isTrue(view.getAuthorId() == profile.getId(), "该文章不属于你");
      model.put("view", view);
    }
    model.put("channels", this.channelService.findAll(0));
    return view("/channel/editing");
  }
  
  @PostMapping({"/submit"})
  public String post(PostVO post, @RequestParam(value="file", required=false) MultipartFile file)
    throws Exception
  {
    Assert.notNull(post, "参数不完整");
    Assert.state(StringUtils.isNotBlank(post.getTitle()), "标题不能为空");
    Assert.state(StringUtils.isNotBlank(post.getContent()), "内容不能为空");
    
    AccountProfile profile = getSubject().getProfile();
    post.setAuthorId(profile.getId());
    if ((file != null) && (!file.isEmpty()))
    {
      String thumbnail = this.fileRepo.storeScale(file, this.appContext.getThumbsDir(), 350, 200);
      if (StringUtils.isNotBlank(post.getThumbnail())) {
        this.fileRepo.deleteFile(post.getThumbnail());
      }
      post.setThumbnail(thumbnail);
    }
    if (post.getId() > 0L)
    {
      PostVO exist = this.postService.get(post.getId());
      Assert.notNull(exist, "文章不存在");
      Assert.isTrue(exist.getAuthorId() == profile.getId(), "该文章不属于你");
      
      this.postService.update(post);
    }
    else
    {
      this.postService.post(post);
    }
    return "redirect:/user?method=posts";
  }
  
  @RequestMapping({"/delete/{id}"})
  @ResponseBody
  public Data delete(@PathVariable Long id)
  {
    Data data = Data.failure("操作失败");
    if (id != null)
    {
      AccountProfile up = getSubject().getProfile();
      try
      {
        this.postService.delete(id.longValue(), up.getId());
        data = Data.success("操作成功", Data.NOOP);
      }
      catch (Exception e)
      {
        data = Data.failure(e.getMessage());
      }
    }
    return data;
  }
}
